package com.DHH.Controller.Base;

import com.DHH.Dto.FilterQuery;
import com.DHH.Entity.Base.BaseLoginRecordType;
import com.DHH.Mapper.Base.BaseLoginRecordMapper;
import com.DHH.Dto.Message;
import com.DHH.Dto.PageInfo;
import com.DHH.Dto.QueryInfo;
import com.DHH.Entity.Base.BaseLoginRecord;
import com.DHH.Mapper.Base.BaseLoginRecordTypeMapper;
import com.DHH.Service.Base.IBaseLoginRecordService;
import com.DHH.Util.ReflectionUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;


/**
 * Created by Administrator on 2017/4/15.
 */
@Controller
@RequestMapping("Admin/Base/BaseLoginRecord")
public class BaseLoginRecordController {
    private Logger logger= LoggerFactory.getLogger(BaseLoginRecordController.class);
    @Autowired
    private BaseLoginRecordMapper baseLoginRecordMapper;
    @Autowired
    private IBaseLoginRecordService baseLoginRecordService;
    @Autowired
    private BaseLoginRecordTypeMapper baseLoginRecordTypeMapper;

    @RequestMapping("/listPage")
    public String listPage(HttpSession session,ModelMap modelMap){
        List<BaseLoginRecordType> baseLoginRecordTypeList=baseLoginRecordTypeMapper.selectAll();
        modelMap.put("baseLoginRecordTypeList",baseLoginRecordTypeList);
        return "admin/baseLoginRecord/listBaseLoginRecordPage.jsp";
    }

    @ResponseBody
    @RequestMapping("/query")
    public PageInfo<BaseLoginRecord> query(QueryInfo queryInfo) {
        System.err.println("--------------查询参数---------------");
        System.err.println(queryInfo);
        System.err.println("--------------查询参数---------------");
        /*为查询设置统一分页条件和排序条件*/
        PageHelper.startPage(queryInfo.getCurrentPage(), queryInfo.getPageSize());
        PageHelper.orderBy(queryInfo.getSort() + " " + queryInfo.getOrder());
        if(queryInfo.getType()==null)
        {
            queryInfo.setType("search");
        }
        if(queryInfo.getCurrentPage()==null)
        {
            queryInfo.setCurrentPage(1);
        }
        if(queryInfo.getPageSize()==null)
        {
            queryInfo.setPageSize(10);
        }
        try{
            if(queryInfo.getType().equals("screen"))
             {
                   return this.screen(queryInfo);
             }
             if(queryInfo.getType().equals("filter"))
            {

                   return this.searchFilter(queryInfo);
            }
             return this.search(queryInfo);
        }catch (Exception e)
        {   e.printStackTrace();
            PageInfo<BaseLoginRecord> p=new PageInfo<BaseLoginRecord>();
            p.setError(e.getMessage());
            p.setMsg("查询失败，请稍后重试！");
            return p;
        }

    }



    /**
     * 查询
     * @param queryInfo
     * @return
     */
    private PageInfo<BaseLoginRecord> search(QueryInfo queryInfo)
    {   System.out.println("---------\n"+queryInfo);

        BaseLoginRecord baseLoginRecord=new BaseLoginRecord();
            /*在这里设置相关的查询参数*/
        PageInfo<BaseLoginRecord> pageInfo =new PageInfo<BaseLoginRecord>();
        if(queryInfo.getSearchKey()==null)
        {
            queryInfo.setSearchKey("");
        }
        baseLoginRecord.setLoginName("%"+queryInfo.getSearchKey()+"%");
        Page<BaseLoginRecord> page = (Page<BaseLoginRecord>) baseLoginRecordMapper.searchByBaseLoginRecord(baseLoginRecord);
        pageInfo.setRows(page.getResult());
        pageInfo.setTotalRow(page.getTotal());
        pageInfo.setCurrentPage(queryInfo.getCurrentPage());
       return pageInfo;
    }

    /**
     * 筛选
     * @param queryInfo
     * @return
     */
    private  PageInfo<BaseLoginRecord> screen(QueryInfo queryInfo) {
    	PageInfo<BaseLoginRecord> pageInfo=new PageInfo< BaseLoginRecord>();
    	 BaseLoginRecord baseLoginRecord=new  BaseLoginRecord();
        for (String key : queryInfo.getMap().keySet()) {
               ReflectionUtils.setFieldValue(baseLoginRecord,key,queryInfo.getMap().get(key));
        }
        Page<BaseLoginRecord> page = (Page<BaseLoginRecord>) baseLoginRecordMapper.screenByBaseLoginRecord(baseLoginRecord);
        pageInfo.setRows(page.getResult());
        pageInfo.setTotalRow(page.getTotal());
        pageInfo.setCurrentPage(queryInfo.getCurrentPage());
        return pageInfo;
    }

    /**
     * 过滤
     * @param queryInfo
     * @return
     */
    private PageInfo<BaseLoginRecord> searchFilter(QueryInfo queryInfo) throws ParseException {    BaseLoginRecord baseLoginRecord=new  BaseLoginRecord();
    	PageInfo<BaseLoginRecord> pageInfo=new PageInfo< BaseLoginRecord>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        FilterQuery filterQuery=new FilterQuery();
        filterQuery.setStartValue(queryInfo.getFilterStartValue());
        filterQuery.setEndValue(sdf.format(new Date(sdf.parse(queryInfo.getFilterEndValue()).getTime() + 24 * 3600 * 1000)));
        Page<BaseLoginRecord> page = (Page<BaseLoginRecord>)baseLoginRecordMapper.filterByBaseLoginRecord(filterQuery);
        pageInfo.setRows(page.getResult());
        pageInfo.setTotalRow(page.getTotal());
        pageInfo.setCurrentPage(queryInfo.getCurrentPage());
        return pageInfo;
    }
}

